/**
 * "LeagueDateUtils.java is for what: "
 * 
 * Create by Ray at 2016年6月29日 下午4:25:40.
 * Ray [email:rayinhangzhou@126.com]
 * 
 *      ┏┛ ┻━━━━━┛ ┻┓
 *      ┃　　　　　　 ┃
 *      ┃　　　━　　　┃
 *      ┃　┳┛　  ┗┳　┃
 *      ┃　　　　　　 ┃
 *      ┃　　　┻　　　┃
 *      ┃　　　　　　 ┃
 *      ┗━┓　　　┏━━━┛
 *        ┃　　　┃   神兽保佑
 *        ┃　　　┃   代码无BUG！
 *        ┃　　　┗━━━━━━━━━┓
 *        ┃　　　　　　　    ┣┓
 *        ┃　　　　         ┏┛
 *        ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛
 *          ┃ ┫ ┫   ┃ ┫ ┫
 *          ┗━┻━┛   ┗━┻━┛
 * Revision History
 *
 * Date            Programmer                   Notes
 * ---------    ---------------------  -----------------------------------
 * 2016年6月29日            Ray                       initial
 **/
package com.souche.employment;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

public class CommonUtils {

	public static String formatDateBetween(Date date) {
		long between = System.currentTimeMillis()- date.getTime();
		int hour = (int) (between / 60 / 60 / 1000);
		if (hour == 0) {
			return "刚刚";
		}
		if (hour < 24) {
			return hour + "小时前";
		}
		int dayCur = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
		Calendar _temp = Calendar.getInstance();
		_temp.setTime(date);
		int day = dayCur - _temp.get(Calendar.DAY_OF_YEAR);
		if (day == 1) {
			return "昨天";
		} else if (day < 0) {
			return (day + 365) + "天前";
		} else {
			return day + "天前";
		}
	}

	public static String format2DecimalStr(double price) {
		if (Double.isNaN(price) || Double.isInfinite(price)) {
			return "0.00";
		}
		return new BigDecimal(price).setScale(2, RoundingMode.HALF_UP).toString();
	}

	public static String formatSalePrice(Integer price) {
		if (price == null || price == -1) {
			return "未定价";
		} else if (price == 0) {
			return "面议";
		}
		return new BigDecimal(price * 1.0 / 10000).setScale(2, RoundingMode.HALF_UP) + "万元";
	}
	
	public static String getIp(HttpServletRequest request) {
		String remoteIp = request.getHeader("X-Forwarded-For");
		if (StringUtils.isBlank(remoteIp)) {
			remoteIp = request.getHeader("X-Real-IP");
		}
		if (StringUtils.isBlank(remoteIp)) {
			remoteIp = request.getRemoteAddr();
		}
		if (remoteIp.contains("0:0:0:0:0:0")) {
			remoteIp = "localhost";
		}
		return remoteIp;
	}

	public static String getRequestUrl(HttpServletRequest request) {
		String requestUrl = request.getHeader("X-Request-Url");
		if (StringUtils.isBlank(requestUrl)) {
			requestUrl = request.getRequestURL().toString();
		}
		return requestUrl;
	}
	
	public static String resolvePrice(Long price) {
        if (null == price || price.longValue() < 0) {
            return "未定价";
        } else if (0 == price) {
            return "面议";
        } else {
           
            return  CommonUtils.format2DecimalStr(price * 1.0 / 10000);
        }
    }


    /**
     * 生成8位短 uuid
     * @return
     */
	public static String getShortUuid() {
        String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
                "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z" };
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();

	}
}
